﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wicture.DbRESTFul;
using Wicture.QPZS.EPC.API.Domain.Entities;

namespace Wicture.QPZS.EPC.API.Throttlings
{
    public class AccountApiRestThrotting : IThrottling
    {
        public decimal Order => 3;
        public const int UnLimitTimes = -99;
        public List<ServicePackageType> ServicePackageTypes => new List<ServicePackageType> { ServicePackageType.包次 };

        public void Invoke(ThrottlingContext context)
        {
            var api = context.AccountPackageApi;
            if (api.Total == UnLimitTimes)
            {
                return;
            }
            if (api.Total <= api.Consumed)
            {
                throw new LogicalException($"{context.ServiceDefinition?.Name}接口余量不足。", 602);
            }
        }
    }
}
